/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-06
 */
package org.jeecg.modules.flow.callback.inbound.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.mapper.WmsInReceiptMapper;
import org.jeecg.modules.business.doc.business.mapper.WmsInReceiptMaterialMapper;
import org.jeecg.modules.business.doc.business.vo.WmsInReceiptPage;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.InboundStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.jeecg.modules.utils.entity.WmsBaseDocument;
import org.jeecg.modules.utils.service.IWmsBaseDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 其他入库过账空接口
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-06
 */
@Component
@Slf4j
@Deprecated
public class WmsReceiptPurchasePost<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsBaseDocumentService wmsBaseDocumentService;
    @Autowired
    private WorkFlowService workFlowService;
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Resource
    private WmsInReceiptMapper wmsInReceiptMapper;
    @Resource
    private WmsInReceiptMaterialMapper wmsInReceiptMaterialMapper;
    @Autowired
    private IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;
    @Autowired
    private InBoundBeanUtils inBoundBeanUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("其他入库过账空接口执行开始");
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsInReceiptPage.class);
        WmsBaseDocument document = wmsBaseDocumentService.getById(baseFlowDto.getInfoId());
        if (document == null) {
            throw new JeecgBootException("未能确定单据类型!");
        }
        int committedRows = wmsBaseDocumentService.updateDocumentStatus(document.getId(), document.getTableName(), "200");
        if (committedRows != 1) {
            throw new JeecgBootException("提交失败,只能提交已保存状态的数据");
        }
        //根据单据id获取任务id
        WmsInReceipt receipt = wmsInReceiptMapper.selectById(baseFlowDto.getInfoId());
        inBoundBeanUtils.updateTaskWareHouseStatus(receipt.getWmsInReceiptTaskId());
        //调用回调
        workFlowService.backAutoCommit(receipt.getWmsInReceiptTaskId(), receipt);
        log.info("其他入库过账空接口执行结束了...");
    }

    /**
     * 修改入库任务的状态
     *
     * @param taskId
     * @return void
     * @author FanZhiwen
     * @since 2023/10/11 10:31
     */
    void updateTaskWareHouseStatus(String taskId) {
        WmsInReceiptTask task = iWmsInReceiptTaskService.getById(taskId);
        List<WmsInReceiptTaskMaterial> materialList = iWmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId)
        );
        //任务总数量
        BigDecimal total = materialList.stream().map(WmsInReceiptTaskMaterial::getPlanQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        //任务下的所有已入库的入库单
        List<WmsInReceipt> inReceipts = wmsInReceiptMapper.selectList(new LambdaQueryWrapper<WmsInReceipt>()
                .eq(WmsInReceipt::getWmsInReceiptTaskId, taskId)
                .eq(WmsInReceipt::getDocumentStatus, DocumentStatusEnum.COMPLETE.getCode())
        );
        List<WmsInReceiptMaterial> inReceiptMaterials = wmsInReceiptMaterialMapper.selectList(new LambdaQueryWrapper<WmsInReceiptMaterial>()
                .in(WmsInReceiptMaterial::getWmsInReceiptId, inReceipts.stream().map(WmsInReceipt::getId).collect(Collectors.toList()))
        );
        BigDecimal decimal = inReceiptMaterials.stream().map(WmsInReceiptMaterial::getWarehouseQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (total.compareTo(decimal) == 0) {
            task.setWarehouseStatus(InboundStatusEnum.ALL.getCode());
            task.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
        }
        if (total.compareTo(decimal) > 0) {
            task.setWarehouseStatus(InboundStatusEnum.PART.getCode());
            task.setDocumentStatus(DocumentStatusEnum.PROCESS.getCode());
        }
        iWmsInReceiptTaskService.updateById(task);
    }
}
